package com.sg.file.transform.provider.event;

import com.sg.file.transform.api.dto.TransformReqeust;
import com.sg.file.transform.provider.center.TransformCodeFactory;
import com.sg.file.transform.provider.queue.TransformQueueProcessor;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 转化事件监听
 * Created by asus on 2018/7/20.
 */
@Component
@Slf4j
public class AsynFileTransformListener implements ApplicationListener<AsynFileTransformEvent> {

    @Autowired
    private TransformCodeFactory transformCodeFactory;

    private TransformQueueProcessor transformQueueProcessor;

    @Override
    public void onApplicationEvent(AsynFileTransformEvent fileTransformEvent) {
        //获取转换请求
        List<TransformReqeust> transformReqeusts = fileTransformEvent.getTransformReqeusts();
        //获取队列处理类
        transformQueueProcessor = TransformQueueProcessor.getInstance();
        //将请求插入队列中
        for(TransformReqeust transformReqeust:transformReqeusts) {
            if(transformReqeust==null){
                continue;
            }
            transformQueueProcessor.enqueue(transformReqeust);
            //记录日志
            log.info("{}-{}-{}-{}-{}：加入文件转化队列",
                    DateTime.now().toString("yyyy-MM-dd HH:mm:ss"),
                    transformReqeust.getFileId()
                    ,transformReqeust.getFileType()
                    ,transformReqeust.getTransformToFileSizeType()
                    ,transformReqeust.getTransformToFileSuffixType());
        }
        //通知队列进行处理
        transformQueueProcessor.transformTaskQueue(transformCodeFactory);
    }
}
